<?php
/**
 * phpRPG
 * Copyright 2011 phpRPG, All Rights Reserved
 * @package phpRPG
 * @author Jake B.
 *
 * Website: http://phprpg.net
 */

class db_mysql{
	/*
	 * Database Type
	 */
	public $type = "MySQL";
	/*
	 * Database Version
	 */
	public $version;
	/*
	 * Are we connected?
	 */
	public $isConnected = 0;
	/*
	 * The connection
	 */
	public $conn;
	/*
	 * Number of queries ran on current page
	 */
	public $queries = 0;
	/*
	 * Last query that was executed
	 */
	public $query;
	/*
	 * Debug Mode
	 */
	public $debug = false;

	/*
	 * Establish a connection to the
	 * DB Server
	 */
	public function connect(){
		global $cfg;
		if($this->isConnected == 0){
			$this->conn = mysql_connect($cfg['database']['host'],$cfg['database']['user'],$cfg['database']['pass']);
			if(mysql_select_db($cfg['database']['name'],$this->conn)){
				$this->isConnected = 1;
			}
		}
	}

	/*
	 * Get current DBVersion
	 */
	public function version(){
		if($this->version){
			return $this->version;
		}
		$query = $this->query("SELECT VERSION() as version");
		$ver = $this->fetchrow($query);
		if($ver['version']){
			$version = explode(".", $ver['version'], 3);
			$this->version = intval($version[0]).".".intval($version[1]).".".intval($version[2]);
		}
		return $this->version;
	}

	/*
	 * Fetch Selected Rows
	 */
	public function fetchrow($query){
		$return = mysql_fetch_assoc($query);
		return $return;
	}

	/*
	 * Return number of rows selected by a query
	 */
	public function recordcount($query){
		return mysql_num_rows($query);
	}

	/*
	 * Insert Id
	 */
	public function insert_id(){
		return mysql_insert_id($this->conn);
	}

	/*
	 * Escape String
	 */
	public function escape($string){
		return mysql_real_escape_string($string,$this->conn);
	}

	/*
	 * Select rows automatically
	 */
	public function select($table,$fields="*",$where="",$other=array()){
		$query = "SELECT ".$fields." FROM <pre>".$table;
		if($where != ""){
			$query .= " WHERE ".$where;
		}
		if($other['order_by']){
			$query .= " ORDER BY ".$other['order_by'];
			if($other['order_dir']){
				$query .= " ".strtoupper($other['order_dir']);
			}
		}
		if($other['limit']){
			$query .= " LIMIT ".$other['limit'];
		}
		return $this->query($query);
	}

	/*
	 * Insert rows automatically
	 */
	public function insert($table,$fields=array()){
		$keys = array();
		$values = array();
		foreach($fields as $key=>$value){
			$keys[] = "`".$key."`";
		
			//Stupid slashes -.-
			if (get_magic_quotes_gpc()){
				$value = stripslashes($value);
			}
				//escape string
			if (is_string($value)){
				$value = '\'' . $this->escape($value) . '\'';
			}else if (is_int($value) || is_float($value)){
				$value = strval($value);
			}else{
				$value = '\'' . $this->escape($value) . '\'';
			}
			$values[] = $value;
		}
		$query = "INSERT INTO <pre>".$table." ( ".implode(",",$keys)." ) VALUES ( ".implode(",",$values)." );";
		return $this->query($query);
	}

	/*
	 * Update rows automatically
	 */
	public function update($table,$fields=array(),$where=""){
		$query = "UPDATE <pre>".$table." SET";
		$parts = array();
		foreach($fields as $key=>$value){
			$key = "`".$key."`";
		
			//Stupid slashes -.-
			if (get_magic_quotes_gpc()){
				$value = stripslashes($value);
			}
				//escape string
			if (is_string($value)){
				$value = '\'' . $this->escape($value) . '\'';
			}else if (is_int($value) || is_float($value)){
				$value = strval($value);
			}else{
				$value = '\'' . $this->escape($val) . '\'';
			}
			$parts[] = $key." = ".$value;
		}
		$updatevals = implode($parts, ",");
		$query .= " ".$updatevals;
		if($where != ""){
			$query .= " WHERE ".$where;
		}
		return $this->query($query);
	}

	/*
	 * execute a MySQL query
	 */
	public function query($query,$params=0){
		//check if we're already connected to DB
		if($this->isConnected == 0){
			$this->connect();
		}
		//parse parameters
		if($params != 0){
			//split the query up
			$parts = explode('?', $query);

			//count the parameters, and the '?'
			$count1 = count($parts);
			$count2 = count($params);
			if ($count1 <= $count2)
				$params = array_slice($params, 0, $count1);

			if ($count1 > ($count2 + 1)){
				$diff = $count2 - $count1;
				array_fill($params, $diff, '?');
			}

			//sanatize parameters
			for ($i = 0; $i < $count2; $i++){
				$val = $params[$i];

				//Stupid slashes -.-
				if (get_magic_quotes_gpc()){
					$val = stripslashes($val);
				}

				//escape string
				if (is_string($val)){
					$val = '\'' . $this->escape($val) . '\'';
				}else if (is_int($val) || is_float($val)){
					$val = strval($val);
				}else{
					$val = '\'' . $this->escape($val) . '\'';
				}

				$params[$i] = $val;
			}

			$query = '';
			for ($i = 0; $i < $count2; $i++){
				$query .= $parts[$i] . $params[$i];
			}
			$query .= $parts[($count1 - 1)];
		}
		$query = str_replace("<pre>",$cfg['database']['pref'],$query);
		//Execute the query
		$result = mysql_query($query,$this->conn);
		$this->query = $query;
		if($this->debug){
			echo $query."<hr>";
		}
		if($result){
			++$this->queries;
			return $result;
		}
	}
}
?>